package com.hxq.library.controller;

import com.hxq.library.entity.User;
import com.hxq.library.service.IUserService;
import com.hxq.library.service.Impl.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;

/**
 * @Author: Ljx
 * @Date: 2021/11/24 13:30
 * @role:
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;utf-8");
        String studentId = req.getParameter("user");
        String studentEmail = req.getParameter("email");
        String studentNumber = req.getParameter("Tel");
        String studentPassword = req.getParameter("pwd");
        String studentPassword1 = req.getParameter("pwd1");
        User user = new User();
        user.setStudentId(studentId);
        user.setStudentMail(studentEmail);
        user.setStudentNumber(studentNumber);
        user.setStudentPassword(studentPassword);
        if (studentPassword.equals(studentPassword1)&&(studentId!=""&&studentEmail!=""&&studentNumber!=""&&studentPassword!="")){
            IUserService service = new UserService();
            boolean login = false;
            try {
                login = service.login(user);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            HttpSession session = req.getSession();
            session.setAttribute("login",login);
            resp.sendRedirect(req.getContextPath()+"booleanLogin.jsp");
        }else {
            req.setAttribute("msg1","输入的值不能为空！！！");
            req.getRequestDispatcher("login.jsp").forward(req,resp);
        }
    }
}
